package top.codedance.iotp.client.plugins.sync.dnp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.client.ClientApplication;
import top.codedance.iotp.client.net.listenner.PluginService;
import top.codedance.iotp.common.executer.SyncAbstractExecuter;
import top.codedance.iotp.common.entity.PluginResultEntity;
import org.apache.log4j.Logger;
import org.ini4j.Ini;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DnpExecuterImpl extends SyncAbstractExecuter {
    private Logger logger = ClientApplication.getLogger(DnpExecuterImpl.class);

    public static ExecutorService pluginServiceWorkerPool = Executors.newCachedThreadPool(); //插件服务线程池
    private static Map<String, PluginService> pluginServiceMap = new HashMap<>(); //插件服务容器

    public synchronized static PluginService registPluginService(String pluginServiceName, PluginService pluginService){
        if(!pluginServiceMap.containsKey(pluginServiceName)){
            pluginServiceMap.put(pluginServiceName, pluginService);
        }
        return pluginService;
    }

    public synchronized static PluginService getPluginService(String pluginServiceName){
        return pluginServiceMap.get(pluginServiceName);
    }

    public synchronized static void removePluginService(String pluginServiceName){
        pluginServiceMap.remove(pluginServiceName);
    }

    @Override
    public PluginResultEntity exec(byte[] data, String taskId) {
        PluginResultEntity result = new PluginResultEntity();
        try {
            String str = new String(data, StandardCharsets.UTF_8);
            JSONObject client = JSONObject.parseObject(str);
            Integer clientId = client.getInteger("id");

            String type = "";
            ClientApplication.initIniConfig();
            if (ClientApplication.ini != null && ClientApplication.ini.containsKey("dnp")) {
                Ini.Section section = ClientApplication.ini.get("dnp");
                if (section.containsKey("type." + clientId)) {
                    type = section.get("type." + clientId);
                } else {
                    throw new Exception("parameter host is empty.");
                }

                PluginService instance = DnpService.getInstance(clientId, getIniConfig());
                if(instance != null) {
                    if(client.containsKey("tags")) {
                        JSONArray jsonArray = client.getJSONArray("tags");
                        result = instance.execute(jsonArray);
                    }
                    if(client.containsKey("data")) {
                        JSONObject d = client.getJSONObject("data");
                        result = instance.execute(d);
                    }
                }else{
                    result.setOk(false);
                    result.setMsg("\"" + type + "\"Protocol type is not support.");
                }
            }else{
                result.setOk(false);
                result.setMsg("Not found DNP config file.");
            }
        }catch (Exception ex){
            logger.debug(ex.getMessage());
            result.setOk(false);
            result.setMsg(ex.getMessage());
        }
        return result;
    }
}
